package org.community.test.config;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.errors.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Configuration
@Data
@Slf4j
public class MinioConfiguration {

    //服务器地址
    @Value("${minio.endpoint}")
    private String endPoint;

    //账号
    @Value("${minio.accessKey}")
    private String accessKey;

    //密码
    @Value("${minio.secretKey}")
    private String secretKey;

    //目标桶
    @Value("${minio.bucket}")
    private String bucketName;

    @Bean//生成客户端实例
    public MinioClient minioClient() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        log.info("minio endPoint ： {}",this.getEndPoint());
        log.info("minio accessKey ： {}",this.getAccessKey());
        log.info("minio secretKey ： {}",this.getSecretKey());
        //初始化客户端信息
        MinioClient minioClient =  MinioClient.builder()
                .endpoint(this.getEndPoint())
                .credentials(this.getAccessKey(),this.getSecretKey())
                .build();
        //初始化存储桶
        String bucketName = this.getBucketName();
        boolean exist = minioClient.bucketExists(BucketExistsArgs
                .builder()
                .bucket(bucketName)
                .build());
        if(!exist){
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());

        }

        return minioClient;
    }






}
